package com.thinkcube.edutainment.fragment;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.domain.data.CourseData;

@EFragment(R.layout.course_work_dashboard)
public class MyCourseDetailDashBoard extends Fragment implements
		OnTabChangeListener {

	private View mRoot;
	private int mCurrentTab;
	private static final String TAGF = "FragmentTabs1";
	public static final String TAB_Q = "recent question";
	public static final String TAB_C = "course work";
	public static final String TAB_T = "todo";

	@ViewById(android.R.id.tabhost)
	TabHost mTabHost;

	TextView cwTitle;

	private CourseData course;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mRoot = inflater.inflate(R.layout.course_work_dashboard, null);
		mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
		cwTitle = (TextView) mRoot.findViewById(R.id.cw_title);
		System.out.println("HERE 1");
		if (getArguments().containsKey("course")) {

			course = getArguments().getParcelable("course");
			cwTitle.setText(course.getCourseTitle());

		}
		setupTabs();
		return mRoot;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setRetainInstance(true);
		// Intent i=getActivity().getIntent();
		System.out.println("HERE 2");
		mTabHost.setOnTabChangedListener(this);
		updateTab(TAB_Q, R.id.recent_questions_tb);
	}

	@Override
	public void onTabChanged(String tabId) {
		Log.d(TAGF, "onTabChanged(): tabId=" + tabId);
		if (TAB_Q.equals(tabId)) {
			updateTab(tabId, R.id.recent_questions_tb);
			mCurrentTab = 0;
			return;
		}
		if (TAB_C.equals(tabId)) {
			updateTab(tabId, R.id.course_work_tb);
			mCurrentTab = 1;
			return;
		}
		if (TAB_T.equals(tabId)) {
			updateTab(tabId, R.id.todo_tb);
			mCurrentTab = 2;
			return;
		}

	}

	private void setupTabs() {
		mTabHost.setup();

		mTabHost.addTab(newTab(TAB_Q, R.string.tab_recent_question,
				R.id.recent_questions_tb));
		mTabHost.addTab(newTab(TAB_C, R.string.tab_course_work,
				R.id.course_work_tb));

		mTabHost.addTab(newTab(TAB_T, R.string.tab_todo, R.id.todo_tb));

	}

	private TabSpec newTab(String tag, int labelId, int tabContentId) {
		Log.d(TAGF, "buildTab(): tag=" + tag);

		View indicator = LayoutInflater.from(getActivity()).inflate(
				R.layout.tab_text,
				(ViewGroup) mRoot.findViewById(android.R.id.tabs), false);
		((TextView) indicator.findViewById(R.id.texttab)).setText(labelId);

		TabSpec tabSpec = mTabHost.newTabSpec(tag);
		tabSpec.setIndicator(indicator);
		tabSpec.setContent(tabContentId);
		return tabSpec;
	}

	RQListFragment rQuestions = null;
	CWListFragment cWork = null;
	TDoListFragment toDo = null;

	private void updateTab(String tabId, int placeholder) {
		FragmentManager fm = getFragmentManager();
		// if (fm.findFragmentByTag(tabId) == null) {

		System.out.println("TAB IDDDDDD >>>>>" + tabId);

		Bundle bundle = new Bundle();
		bundle.putString("tab_id", tabId);
		bundle.putParcelable("course", course);

		if (TAB_Q.equals(tabId)) {
			if (rQuestions == null) {
				rQuestions = new RQListFragment_();
				rQuestions.setArguments(bundle);
			}

			fm.beginTransaction().replace(placeholder, rQuestions, tabId)
					.commit();

		}

		else if (TAB_C.equals(tabId)) {
			if (cWork == null) {
				cWork = new CWListFragment_();
				cWork.setArguments(bundle);
			}
			fm.beginTransaction().replace(placeholder, cWork, tabId).commit();
		}

		else if (TAB_T.equals(tabId)) {
			if (toDo == null) {
				toDo = new TDoListFragment_();
				toDo.setArguments(bundle);
			}
			fm.beginTransaction().replace(placeholder, toDo, tabId).commit();
		}

	}

	@Click({ R.id.goto_course,R.id.forum,R.id.wiki,R.id.glossary })
	void onClick(View view) {
		
		boolean fromDashBord=true;
		
		Intent intent;
		
	    FragmentTransaction fragmentTransaction = getFragmentManager()
				.beginTransaction();
	    
		switch (view.getId()) {
		case R.id.goto_course:
			
				Bundle bundle = new Bundle();
				CourseViewFragment coursview = new CourseViewFragment_();
				bundle.putParcelable("course", course);
				coursview.setArguments(bundle);
				fragmentTransaction.replace(R.id.myfragment, coursview,"newfrag");
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			
			
			
				/*Bundle bundle = new Bundle();
				bundle.putParcelable("lessonContents", lessonsList.get(position));
				bundle.putInt("pos", position);
			
				bundle.putParcelable("course", courseData);
			
				FollowCourseFragment followCourse = new FollowCourseFragment();
			
				followCourse.setArguments(bundle);
			
				fragmentTransaction.replace(R.id.myfragment, followCourse);
			
				fragmentTransaction.addToBackStack(null);
			
				fragmentTransaction.commit();*/
			
			break;
			
		case R.id.forum:
			
				ForumListFragment forumFragment=new ForumListFragment_();
				Bundle forumBundle=new Bundle();
				forumBundle.putBoolean("fromDashBord", fromDashBord);
				forumBundle.putParcelable("Course", course);
				forumFragment.setArguments(forumBundle);
				fragmentTransaction.replace(R.id.pane, forumFragment);
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			
			break;
		case R.id.wiki:
			
				WikiFragment wikiFragment=new WikiFragment_();
				Bundle wikiBundle=new Bundle();
				wikiBundle.putBoolean("fromDashBord", fromDashBord);
				wikiBundle.putParcelable("Course", course);
				wikiFragment.setArguments(wikiBundle);
				fragmentTransaction.replace(R.id.pane, wikiFragment);
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
				
			break;

		case R.id.glossary:
						
				GlossaryFragment glossaryFragment=new GlossaryFragment_();
				Bundle glossaryBundle=new Bundle();
				glossaryBundle.putParcelable("Course", course);
				glossaryBundle.putBoolean("fromDashBord", fromDashBord);
				glossaryFragment.setArguments(glossaryBundle);
				fragmentTransaction.replace(R.id.pane, glossaryFragment);
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			
			break;
		default:
			break;
		}
	}

}
